package ro.imc.rest.client.display;

import java.util.ArrayList;
import java.util.List;

import ro.imc.rest.client.display.components.CoursesFrom;
import ro.imc.rest.client.display.components.LearningObjectFrom;
import ro.imc.rest.client.display.util.DialogForm;
import ro.imc.rest.client.display.util.MessageConstants;
import ro.imc.rest.client.services.forserver.RestService;
import ro.imc.rest.client.services.forserver.RestServiceAsync;
import ro.imc.rest.shared.dto.LearningObject;
import ro.imc.rest.shared.dto.LearningObjectStatus;
import ro.imc.rest.shared.dto.LearningObjectType;
import ro.imc.rest.shared.dto.RestLogin;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.NotificationMole;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.TextArea;

public class ScreenForm extends Composite {

	private static ScreenFormUiBinder uiBinder = GWT
			.create(ScreenFormUiBinder.class);

	interface ScreenFormUiBinder extends UiBinder<Widget, ScreenForm> {
	}

	public ScreenForm() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@UiField AbsolutePanel componentsPanel;
	@UiField static TextArea logArea;
	@UiField AbsolutePanel navigationPanel;
	@UiField AbsolutePanel infoAreaPanel;
	@UiField Button catalogBtn;
	@UiField Button courseBtn;
	@UiField Button searchBtn;
	
	private RestLogin user;
	private NotificationMole mole = new NotificationMole();
	private RestServiceAsync service = GWT.create(RestService.class);
	public ScreenForm(RestLogin user) {
		initWidget(uiBinder.createAndBindUi(this));
		this.user = user;
	}

	@UiHandler("catalogBtn")
	void onCatalogClick (ClickEvent event) {
		componentsPanel.clear();
		logArea.setText(logArea.getText() + "\nNavi: Catalogs click!");
		Tree tree = new Tree();
        TreeItem outerRoot = new TreeItem(new LearningObjectFrom(new LearningObject("Catalog1", LearningObjectType.CATALOG, "azi", "maine")));
        TreeItem outerRoot2 = new TreeItem(new LearningObjectFrom(new LearningObject("Catalog2", LearningObjectType.CATALOG, "azi", "maine")));
        TreeItem outerRoot3 = new TreeItem(new LearningObjectFrom(new LearningObject("Catalog3", LearningObjectType.CATALOG, "azi", "maine")));
        outerRoot.addItem(outerRoot2);
        outerRoot.addItem(outerRoot3);
        tree.addItem(outerRoot);
        componentsPanel.add(tree);
	
		
	}
	@UiHandler("courseBtn")
	void onCoursesClick (ClickEvent event) {
		componentsPanel.clear();
		logArea.setText(logArea.getText() + "\nNavi: Course click!");
		service.getCoursesList(user, new AsyncCallback<List<LearningObject>>() {
			
			@Override
			public void onSuccess(List<LearningObject> result) {
				List<LearningObject> list = new ArrayList<LearningObject>();
				list.add(new LearningObject("COURSE1", LearningObjectType.COURSE, "azi1", "maine1", "23111","acasa1", LearningObjectStatus.BOOKED, "descriere 1", new ArrayList<String>()));
				list.add(new LearningObject("COURSE2", LearningObjectType.COURSE, "azi2", "maine2", "23112","acasa2", LearningObjectStatus.STARTED, "descriere 2", new ArrayList<String>()));
				list.add(new LearningObject("COURSE3", LearningObjectType.COURSE, "azi3", "maine3", "23113","acasa3", LearningObjectStatus.FINISHED, "descriere 3", new ArrayList<String>()));
				componentsPanel.add(new CoursesFrom(user, list));
			}
			
			@Override
			public void onFailure(Throwable caught) {
				DialogForm.displayDialog(MessageConstants.REST_ERR_TITLE, 
									caught.getMessage(),
									MessageConstants.REST_ERR_MESSAGE, 
									MessageConstants.REST_ERR_TIP);
				
			}
		});
	}
	@UiHandler("searchBtn")
	void onSearchesClick (ClickEvent event) {
		componentsPanel.clear();
		logArea.setText(logArea.getText() + "\nNavi: Search click!");
	}
}
